package org.dbstar.scheduler.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dbstar.scheduler.beans.CompoundJob;
import org.dbstar.scheduler.beans.CompoundJobConf;
import org.dbstar.scheduler.beans.Job;
import org.dbstar.scheduler.beans.JobConf;
import org.dbstar.scheduler.beans.JobDefine;
import org.dbstar.scheduler.beans.LoopJobConf;
import org.dbstar.scheduler.beans.OperateType;
import org.dbstar.scheduler.control.CatalogController;
import org.dbstar.scheduler.control.JobConfController;
import org.dbstar.scheduler.control.JobController;
import org.dbstar.scheduler.identifier.Identifier;


/**
 * 显示Job的执行结果页面
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-4-5
 */
public class JobResultAction extends ActionSupport {
	private CatalogController catalogController;
	private JobController jobController;
	private JobConfController jobConfController;

	private Identifier identifier;

	private Job job;
	private JobConf conf;
	private List<Map<String, Object>> subJobs;
	private boolean showDependency;
	private boolean showCondition;

	public CatalogController getCatalogController() {
		return catalogController;
	}

	public void setCatalogController(CatalogController catalogController) {
		this.catalogController = catalogController;
	}

	public void setJobController(JobController jobController) {
		this.jobController = jobController;
	}

	public JobController getJobController() {
		return jobController;
	}

	public JobConfController getJobConfController() {
		return jobConfController;
	}

	public void setJobConfController(JobConfController jobConfController) {
		this.jobConfController = jobConfController;
	}

	public void setIdentifier(Identifier identifier) {
		this.identifier = identifier;
	}

	public Job getJob() {
		return job;
	}

	public JobConf getConf() {
		return conf;
	}

	public List<Map<String, Object>> getSubJobs() {
		return subJobs;
	}

	public boolean isShowDependency() {
		return showDependency;
	}

	public boolean isShowCondition() {
		return showCondition;
	}

	@Override
	public String execute() throws Exception {
		if (identifier != null) {
			job = jobController.getJob(identifier);
			if (job != null) {
				conf = jobConfController.getJobConf(job.getJobConfIdentifier());
				if (CompoundJobConf.JOB_TYPE.equals(job.getType())) {
					subJobs = new ArrayList<Map<String, Object>>();
					for (JobDefine jjd : ((CompoundJob) job).getSubJobs()) {
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("jjd", jjd);
						JobDefine cjd = null;
						if (CompoundJobConf.JOB_TYPE.equals(conf.getType())) {
							cjd = ((CompoundJobConf) conf).getJob(subJobs.size());
						} else if (LoopJobConf.JOB_TYPE.equals(conf.getType())) {
							cjd = ((LoopJobConf) conf).getSubJob();
						}
						map.put("cjd", cjd);
						if (!jjd.isSkip()) {
							Job subJob = jobController.getJob(jjd.getIdentifier());
							map.put("job", subJob);
							map.put("conf", jobConfController.getJobConf(subJob.getJobConfIdentifier()));
						} else {
							map.put("conf", jobConfController.getJobConf(cjd.getIdentifier()));
						}
						if (jjd.getCondition() != null) {
							showCondition = true;
						}
						if (jjd.getDependencies() != null) {
							showDependency = true;
						}
						subJobs.add(map);
					}
				}
				operate(OperateType.job_result_view, identifier);
				return job.getType();
			}
		}
		return ERROR;
	}
}
